{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import tree\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"cart.csv\", delimiter=\",\")\n",
    "x_data = data[1:,1:-1]\n",
    "y_data = data[1:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n",
       "            max_features=None, max_leaf_nodes=None,\n",
       "            min_impurity_split=1e-07, min_samples_leaf=1,\n",
       "            min_samples_split=2, min_weight_fraction_leaf=0.0,\n",
       "            presort=False, random_state=None, splitter='best')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建决策树模型\n",
    "model = tree.DecisionTreeClassifier()\n",
    "# 输入数据建立模型\n",
    "model.fit(x_data, y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cart.pdf'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导出决策树\n",
    "import graphviz # http://www.graphviz.org/\n",
    "\n",
    "dot_data = tree.export_graphviz(model, \n",
    "                                out_file = None, \n",
    "                                feature_names = ['house_yes','house_no','single','married','divorced','income'],\n",
    "                                class_names = ['no','yes'],\n",
    "                                filled = True,\n",
    "                                rounded = True,\n",
    "                                special_characters = True)\n",
    "graph = graphviz.Source(dot_data)\n",
    "graph.render('cart')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"289pt\" height=\"433pt\"\r\n",
       " viewBox=\"0.00 0.00 289.00 433.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 429)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-429 285,-429 285,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#e58139\" fill-opacity=\"0.572549\" stroke=\"black\" d=\"M210,-425C210,-425 131,-425 131,-425 125,-425 119,-419 119,-413 119,-413 119,-354 119,-354 119,-348 125,-342 131,-342 131,-342 210,-342 210,-342 216,-342 222,-348 222,-354 222,-354 222,-413 222,-413 222,-419 216,-425 210,-425\"/>\r\n",
       "<text text-anchor=\"start\" x=\"128.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">married ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"137\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.42</text>\r\n",
       "<text text-anchor=\"start\" x=\"127\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 10</text>\r\n",
       "<text text-anchor=\"start\" x=\"130\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [7, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"138\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M154,-306C154,-306 63,-306 63,-306 57,-306 51,-300 51,-294 51,-294 51,-235 51,-235 51,-229 57,-223 63,-223 63,-223 154,-223 154,-223 160,-223 166,-229 166,-235 166,-235 166,-294 166,-294 166,-300 160,-306 154,-306\"/>\r\n",
       "<text text-anchor=\"start\" x=\"59\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">house_no ≤ 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"79.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"69\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 6</text>\r\n",
       "<text text-anchor=\"start\" x=\"68\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [3, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"76\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M148.989,-341.907C144.373,-333.195 139.445,-323.897 134.674,-314.893\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"137.748,-313.218 129.973,-306.021 131.562,-316.496 137.748,-313.218\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"122.63\" y=\"-326.218\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 6 -->\r\n",
       "<g id=\"node7\" class=\"node\"><title>6</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M269,-298.5C269,-298.5 196,-298.5 196,-298.5 190,-298.5 184,-292.5 184,-286.5 184,-286.5 184,-242.5 184,-242.5 184,-236.5 190,-230.5 196,-230.5 196,-230.5 269,-230.5 269,-230.5 275,-230.5 281,-236.5 281,-242.5 281,-242.5 281,-286.5 281,-286.5 281,-292.5 275,-298.5 269,-298.5\"/>\r\n",
       "<text text-anchor=\"start\" x=\"203.5\" y=\"-283.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"193\" y=\"-268.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n",
       "<text text-anchor=\"start\" x=\"192\" y=\"-253.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [4, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"200\" y=\"-238.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;6 -->\r\n",
       "<g id=\"edge6\" class=\"edge\"><title>0&#45;&gt;6</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M192.011,-341.907C197.858,-330.873 204.204,-318.898 210.099,-307.773\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"213.335,-309.142 214.924,-298.667 207.149,-305.864 213.335,-309.142\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"222.267\" y=\"-318.864\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M85,-179.5C85,-179.5 12,-179.5 12,-179.5 6,-179.5 0,-173.5 0,-167.5 0,-167.5 0,-123.5 0,-123.5 0,-117.5 6,-111.5 12,-111.5 12,-111.5 85,-111.5 85,-111.5 91,-111.5 97,-117.5 97,-123.5 97,-123.5 97,-167.5 97,-167.5 97,-173.5 91,-179.5 85,-179.5\"/>\r\n",
       "<text text-anchor=\"start\" x=\"19.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"9\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n",
       "<text text-anchor=\"start\" x=\"8\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [2, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"16\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 1&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>1&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M87.6833,-222.907C82.0246,-211.873 75.8834,-199.898 70.1783,-188.773\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"73.1863,-186.968 65.5087,-179.667 66.9575,-190.162 73.1863,-186.968\"/>\r\n",
       "</g>\r\n",
       "<!-- 3 -->\r\n",
       "<g id=\"node4\" class=\"node\"><title>3</title>\r\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.666667\" stroke=\"black\" d=\"M210,-187C210,-187 127,-187 127,-187 121,-187 115,-181 115,-175 115,-175 115,-116 115,-116 115,-110 121,-104 127,-104 127,-104 210,-104 210,-104 216,-104 222,-110 222,-116 222,-116 222,-175 222,-175 222,-181 216,-187 210,-187\"/>\r\n",
       "<text text-anchor=\"start\" x=\"123\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">income ≤ 77.5</text>\r\n",
       "<text text-anchor=\"start\" x=\"131\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.375</text>\r\n",
       "<text text-anchor=\"start\" x=\"129\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n",
       "<text text-anchor=\"start\" x=\"128\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"132.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 1&#45;&gt;3 -->\r\n",
       "<g id=\"edge3\" class=\"edge\"><title>1&#45;&gt;3</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M129.317,-222.907C133.738,-214.286 138.454,-205.09 143.026,-196.175\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"146.271,-197.516 147.72,-187.021 140.043,-194.322 146.271,-197.516\"/>\r\n",
       "</g>\r\n",
       "<!-- 4 -->\r\n",
       "<g id=\"node5\" class=\"node\"><title>4</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M147,-68C147,-68 74,-68 74,-68 68,-68 62,-62 62,-56 62,-56 62,-12 62,-12 62,-6 68,-0 74,-0 74,-0 147,-0 147,-0 153,-0 159,-6 159,-12 159,-12 159,-56 159,-56 159,-62 153,-68 147,-68\"/>\r\n",
       "<text text-anchor=\"start\" x=\"81.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"71\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"start\" x=\"70\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 0]</text>\r\n",
       "<text text-anchor=\"start\" x=\"78\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = no</text>\r\n",
       "</g>\r\n",
       "<!-- 3&#45;&gt;4 -->\r\n",
       "<g id=\"edge4\" class=\"edge\"><title>3&#45;&gt;4</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M146.903,-103.726C142.313,-95.0615 137.459,-85.8962 132.842,-77.1802\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"135.912,-75.4982 128.138,-68.2996 129.726,-78.7748 135.912,-75.4982\"/>\r\n",
       "</g>\r\n",
       "<!-- 5 -->\r\n",
       "<g id=\"node6\" class=\"node\"><title>5</title>\r\n",
       "<path fill=\"#399de5\" stroke=\"black\" d=\"M262,-68C262,-68 189,-68 189,-68 183,-68 177,-62 177,-56 177,-56 177,-12 177,-12 177,-6 183,-0 189,-0 189,-0 262,-0 262,-0 268,-0 274,-6 274,-12 274,-12 274,-56 274,-56 274,-62 268,-68 262,-68\"/>\r\n",
       "<text text-anchor=\"start\" x=\"196.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"start\" x=\"186\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n",
       "<text text-anchor=\"start\" x=\"185\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 3]</text>\r\n",
       "<text text-anchor=\"start\" x=\"189.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = yes</text>\r\n",
       "</g>\r\n",
       "<!-- 3&#45;&gt;5 -->\r\n",
       "<g id=\"edge5\" class=\"edge\"><title>3&#45;&gt;5</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M189.725,-103.726C194.235,-95.0615 199.006,-85.8962 203.543,-77.1802\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"206.653,-78.7859 208.166,-68.2996 200.444,-75.5537 206.653,-78.7859\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x22442de8d30>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
